package com.gxuwz.medical.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.gxuwz.medical.dao.PersonDao;
import com.gxuwz.medical.domain.family.Family;
import com.gxuwz.medical.domain.person.Person;
import com.gxuwz.medical.domain.vo.AutocompleteModel;
import com.gxuwz.medical.tools.DateUtil;

/**
 * 参合农民档案控制模块
 * @author 演示
 *
 */
public class PersonSerlvet extends BaseServlet {

	private PersonDao personDao;
	@Override
	public void init() throws ServletException {
		personDao=new PersonDao();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m =req.getParameter("m");
		try {
		if("list".equals(m)){
		  	process(req, resp, "/page/person/person_list.jsp");
		}else if("get".equals(m)){
			process(req, resp, "/page/person/person_edit.jsp");
		}else if("del".equals(m)){
			process(req, resp, "/page/person/person_list.jsp");
		}else if("move".equals(m)){
			process(req, resp, "/page/person/person_list.jsp");
		}else if("edit".equals(m)){
			this.edit(req, resp);
			process(req, resp, "/page/person/person_list.jsp");
		}else if("find".equals(m)){
			this.find(req, resp);
		}
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	/**
	 * 根据姓名查找个人信息列表
	 * @param req
	 * @param resp
	 * @throws Exception
	 */
	private void find(HttpServletRequest req, HttpServletResponse resp)
			throws Exception{
		String town =req.getParameter("town");
		String q =req.getParameter("q");
		//插件请求方式是GET，采用转码方法处理中文乱码
		String name =new String(q.getBytes("ISO8859-1"),"utf-8");
	    List<Person> persons=personDao.findByKeyWord(name, town);
	    //将查询结果列表转换为自动填充数据模型列表
	    List<AutocompleteModel> itemList=new ArrayList<AutocompleteModel>();
		for(Person m:persons){
			String itemId=m.getPerscode();
			String itemName =m.getPersname();
			String sex="1".equals(m.getSex())?"男":"女";
			String otherName=sex+"-"+m.getLiveAddress();
			AutocompleteModel item=new AutocompleteModel(itemId, itemName,otherName);
			itemList.add(item);
		}
		//转换为JSON的格式
	    String respBody = JSON.toJSONString(itemList);
		resp.setCharacterEncoding("utf-8");
		PrintWriter out = resp.getWriter();
		out.print(respBody);
		out.flush();
		out.close();
	} 
	
	private void edit(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {
	//1:获取档案信息
	    String famicode=req.getParameter("famicode");
	 //从session对象中获取当前用户名称
	 String creator=(String)req.getSession().getAttribute("userName");
	 //1.1实例化新家庭档案对象
	 Family family =new Family();
	 family.setFamicode(famicode);
	 //2:获取家庭成员个人信息
	 String perscode=req.getParameter("perscode");//农合证号
	 String ruralCard=req.getParameter("ruralCard");//医疗卡号
	 String cardID=req.getParameter("cardID");
	 String persname=req.getParameter("persname");
	 String sex=req.getParameter("sex");
	 String birthday=req.getParameter("birthday");
	 String persPro=req.getParameter("persPro");
	 String isRural=req.getParameter("isRural");
	 String occupation=req.getParameter("occupation");
	 String workUnit=req.getParameter("workUnit");
	 String liveAddress=req.getParameter("liveAddress");
	 String telephone=req.getParameter("telephone");
	 String relation =req.getParameter("relation");
	 
	 //2.1实例化家庭成员个人档案对象
	 Person member=new Person();
	 member.setPerscode(perscode);
	 member.setRuralCard(ruralCard);
	 member.setCardID(cardID);
	 member.setPersname(persname);
	 member.setSex(sex);	 
	 member.setBirthday(birthday);
	 //调用日期组件计算年龄
	 member.setAge(DateUtil.diffYear(birthday));
	 member.setPersPro(persPro);
	 member.setRural("1".equals(isRural)?true:false);
	 member.setOccupation(occupation);
	 member.setWorkUnit(workUnit);
	 member.setLiveAddress(liveAddress);
	 member.setTelephone(telephone);
	 member.setRelation(relation);
	 member.setFamily(family);
	 member.setCreator(creator);
	 member.setCreattime(new java.util.Date());
	 //保存家庭成员信息
	 member.editMember();
	}

	

}
